﻿namespace HCore
{
    public partial class Server
    {
        private class ServerGlobalState : State<Server>
        {
            public ServerGlobalState(Server owner) : base(owner)
            {
            }

            public override void Enter()
            {
                Server server = _owner;
                server.Log("Enter GlobalState");
                _owner._networkMgr.Register(PacketType.RTT, RTTHandle);
            }

            private void RTTHandle(SessionPacket sessionPacket)
            {
                var networkMgr = _owner._networkMgr;
                var clientProxies = _owner._clientProxies;
                uint sessionId = sessionPacket.sessionId;
                if (!clientProxies.ContainsKey(sessionId))
                {
                    // 不处理新玩家了
                    return;
                }

                // RTT包直接返回该玩家
                InputMemoryBitStream intput = sessionPacket.input;
                OutputMemeryBitStream output = new OutputMemeryBitStream();
                output.WriteBytes(intput.GetBuffer(), intput.GetByteLength());
                networkMgr.Send(output, sessionId);
            }

            public override void Execute()
            {
            }

            public override void Exit()
            {
                _owner._networkMgr.Unregister(PacketType.RTT);
                Server server = _owner;
                server.Log("Exit GlobalState");
            }
        }
    }
}